﻿using System;
using System.Threading;
using System.Windows;
using Agent.Messaging;
using Microsoft.Phone.BackgroundAudio;
using System.Windows.Threading;
using WindowsPhoneAudioStreaming.Audio.Streaming;
using Samples.SimpleStreaming.Agent;
using System.IO;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;

namespace Samples.SimpleStreaming
{
    public partial class MainPage
    {
        readonly DispatcherTimer timer;

        public MainPage()
        {
            InitializeComponent();

            // Create a timer to periodically read from log file
            timer = new DispatcherTimer
                        {
                            Interval = TimeSpan.FromSeconds(15)
                        };
            timer.Tick += LogReadTimerTick;

            SimpleAudioStreamer ss = new SimpleAudioStreamer();
        }


        private void LogReadTimerTick(object sender, EventArgs e)
        {
            ReadDataLog();
        }



        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            
            // Read from the log file - this will automatically start the log read timer when done
            ReadDataLog();
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Stop the timer to prevent it running whilst app in background or on different page
            timer.Stop();
        }

        private void StartClick(object sender, RoutedEventArgs e)
        {
            BackgroundAudioPlayer.Instance.Play();
        }

        private void StopClick(object sender, RoutedEventArgs e)
        {
            BackgroundAudioPlayer.Instance.Stop();
        }

        private void ReadLogClick(object sender, RoutedEventArgs e)
        {
            ReadDataLog();
        }

        private void ReadDataLog()
        {
            timer.Stop();
            ThreadPool.QueueUserWorkItem(asyncobj =>
                                             {
                                                 var du = new DataUsage();
                                                 du.RefreshDataUsage();
                                                 Dispatcher.BeginInvoke(() =>
                                                                            {
                                                                                ContentPanel.DataContext = du;
                                                                                timer.Start();
                                                                            });
                                             });
            
        }

        private void ClearLogClick(object sender, RoutedEventArgs e)
        {
            var messageSender = new MessageSender("AudioAgent");
            messageSender.Start();
            messageSender.SendMessage("ClearLog");
            messageSender.Stop();

        }

        private void TraceLogClick(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/TraceLogPage.xaml", UriKind.Relative));
        }

        private void BtnSettings_Click(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }
    }
}